package com.xueyi.aixihu.mapper;

import com.xueyi.aixihu.entity.vo.DtAreaOrVillageVo;
import com.xueyi.aixihu.entity.vo.DtFindByAreaVo;
import com.xueyi.aixihu.entity.vo.SelectVo;
import com.xueyi.aixihu.reqeust.DtAddVillageReq;
import com.xueyi.aixihu.reqeust.DtFindByAreaAndVillageReq;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author yuyu
 * @date : 2022/8/17
 * @Description :村/行政区管理
 */
public interface DtAreaManageMapper {
    /**
     * 查询所有行政村社区的集合
     *
     * @return
     */
    @Select("<script>" +
            " SELECT *  FROM dt_news_sort  WHERE pid IN " +
            " (SELECT id  FROM dt_news_sort   ) and is_valid != 2 " +
            " <if test = ' dtFindByAreaAndVillageReq.name != null ' > " +
            " and name like concat ('%',#{dtFindByAreaAndVillageReq.name},'%') " +
            " </if> " +
            " <if test = ' dtFindByAreaAndVillageReq.type != null ' > " +
            " and pid = #{dtFindByAreaAndVillageReq.type} " +
            " </if> " +
            " <if test = ' dtFindByAreaAndVillageReq.isValid != null ' > " +
            " and is_valid = #{dtFindByAreaAndVillageReq.isValid} " +
            " </if> " +
            " order by create_time desc " +
            " limit #{dtFindByAreaAndVillageReq.startIndex},#{dtFindByAreaAndVillageReq.limit} " +
            "</script>")
    List<DtFindByAreaVo> findByAreaAndVillage(@Param("dtFindByAreaAndVillageReq") DtFindByAreaAndVillageReq dtFindByAreaAndVillageReq);

    /**
     * 查询所有行政村社区的集合总条数
     *
     * @return
     */
    @Select("<script>" +
            " SELECT count(id)  FROM dt_news_sort  WHERE pid IN " +
            " (SELECT id  FROM dt_news_sort  ) and is_valid != 2 " +
            " <if test = ' dtFindByAreaAndVillageReq.name != null ' > " +
            " and name like concat ('%',#{dtFindByAreaAndVillageReq.name},'%') " +
            " </if> " +
            " <if test = ' dtFindByAreaAndVillageReq.type != null ' > " +
            " and pid = #{dtFindByAreaAndVillageReq.type} " +
            " </if> " +
            " <if test = ' dtFindByAreaAndVillageReq.isValid != null ' > " +
            " and is_valid = #{dtFindByAreaAndVillageReq.isValid} " +
            " </if> " +
            "</script>")
    Integer findByAreaAndVillageCount(@Param("dtFindByAreaAndVillageReq") DtFindByAreaAndVillageReq dtFindByAreaAndVillageReq);

    /**
     * 分别查询村/社区的数量
     *
     * @return
     */
    @Select(" SELECT " +
            " COUNT(IF(pid = 4,id,NULL)) messageTotal, " +
            " COUNT(IF(pid = 3,id,NULL)) villageTotal, " +
            " COUNT(IF(pid = 16,id,NULL)) areaTotal" +
            " FROM " +
            " dt_news_sort " +
            " where is_valid != 2 ")
    DtAreaOrVillageVo findByAreaOrVillageCount();

    /**
     * 添加村/社区
     */
    @Insert(" insert into dt_news_sort (name,pid,create_time,synopsis,contacts,phone,image_url) " +
            " values(#{dtAddVillageReq.name},#{dtAddVillageReq.pid},#{dtAddVillageReq.createTime} " +
            " ,#{dtAddVillageReq.synopsis},#{dtAddVillageReq.contacts},#{dtAddVillageReq.phone},#{dtAddVillageReq.imageUrl}) ")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    Integer addVillage(@Param("dtAddVillageReq") DtAddVillageReq dtAddVillageReq);

    /**
     * 查询单个村/社区数据
     *
     * @param id
     * @return
     */
    @Select(" select * from dt_news_sort where id = #{id} ")
    DtFindByAreaVo findByOneArea(Integer id);

    /**
     * 修改状态
     *
     * @param id
     * @param isValid
     */
    @Update(" update dt_news_sort set is_valid = #{isValid} where id = #{id} ")
    void updateIsValid(@Param("id") Integer id,
                       @Param("isValid") Integer isValid);


    /**
     * 查询二级父类信息
     *
     * @return
     */
    @Select(" select id,name from dt_news_sort where pid = 1 ")
    List<SelectVo> findByParent();

    /**
     * 查询是否有该名字的村或社区
     *
     * @param name
     * @return
     */
    @Select(" select count(id) from dt_news_sort where name = #{name} and is_valid != 2 ")
    Integer findByVillageName(String name);

    /**
     * 修改村镇信息
     * @param dtFindByAreaVo
     */
    @Update("<script>" +
            " update dt_news_sort set name = #{name},pid = #{pid} ,synopsis = #{synopsis} ," +
            " contacts = #{contacts} ,phone = #{phone},image_url = #{imageUrl} where id = #{id} " +
            "</script>")
    void updateVillageInformation(DtFindByAreaVo dtFindByAreaVo);



}
